﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Services.Validation;
using IRepositories;
using LinqRepositories;
using IServices;
using GestionLinqLibData;


namespace Services
{
    public class EleveService : IEleveService
    {

        private IValidationDictionary _validationDictionary;
        private IEleveRepository _repository;

        public EleveService(IValidationDictionary validationDictionary)
            : this(validationDictionary, new LinqEleveRepository())
        { }

        public EleveService(IValidationDictionary validationDictionary, IEleveRepository repository)
        {
            _validationDictionary = validationDictionary;
            _repository = repository;


        }


        protected bool ValidateEleve(eleve eleveToValidate)
        {
            if (eleveToValidate.nom_eleve.Trim().Length == 0)
            {
                _validationDictionary.AddError("nom_eleve", "nom nul");
            }
            if (eleveToValidate.prenom_eleve.Trim().Length == 0)
            {
                _validationDictionary.AddError("prenom_eleve", "prenom nul");
            }
            return _validationDictionary.IsValid;
        }

      
        #region IEleveService Membres

        public bool CreateEleve(eleve EleveToCreate)
        {
            if (!ValidateEleve(EleveToCreate))
                return false;

            try
            {
                _repository.CreateEleve(EleveToCreate);
            }
            catch
            {

                return false;
            }
            return true;
           
                
        }

        public eleve GetEleve(int num_inscri)
        {
            return _repository.GetEleve(num_inscri);
        }

        public bool EditEleve(eleve eleveToEdit)
        {
            if (!ValidateEleve(eleveToEdit))
                return false;

            try
            {
                _repository.EditEleve(eleveToEdit);
               
            }
            catch(Exception e)
            {
                _validationDictionary.AddError("Erreur", e.Message);
                return false;
            }
            return true;
        }

        public bool DeleteEleve(eleve eleveToDelete)
        {
            try
            {
                _repository.DeleteEleve(eleveToDelete);
            }
            catch
            {

                return false;
            }
            return true;
        }

       


        public IEnumerable<eleve> ListEleves()
        {
            return _repository.ListEleves();
        }

        #endregion
    }
}
